﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace Grizzy_X
{
    public class CTextLayer : CLayer
    {
        public List<CText> Text_Set;
        public Bitmap txlayer;

        public bool NewTextLayer(int id, string name, string path, string des)
        {
            try
            {
                ID = id;
                Name = name;
                Path = path;
                Layer_Type = LayerType.CTextLayer;
                bSelected = true;
                bVisible = true;
                Text_Set = new List<CText>();
                Description = des;
                return true;
            }
            catch
            {
                return false;
            }
        }

        //添加位置信息
        public bool AddText(CText NewText)
        {
            try
            {
                Text_Set.Add(NewText);
                return true;
            }
            catch
            {
                return false;
            }
        }

        //文本的位置如何存储？？？？
        public bool DeleteText()
        {
            try
            {
                for (int i = 0; i < Controller.Selected.Count(); i++)
                {
                    Text_Set.RemoveAt(Controller.Selected[i]);
                    Text_Set.Insert(Controller.Selected[i], new CText());
                    Text_Set[Controller.Selected[i]].points = new List<PointF>();
                }
                for (int i = 0; i <Text_Set.Count(); i++)
                {
                    if (Text_Set[i].points.Count() == 0)
                    {
                        Text_Set.RemoveAt(i);
                        i--;
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        //实现方法有待考虑
        public bool Union()
        {
            CText temptx = new CText();
            int id = Text_Set[Controller.Selected[0]].GetID();
            temptx.newCtext(id, Text_Set[Controller.Selected[0]].LegendID);
            for (int i = 0; i < Controller.Selected.Count(); i++)
            {
                for (int j = 0; j < Text_Set[Controller.Selected[i]].points.Count(); j++)
                {
                    temptx.addpoint_text(Text_Set[Controller.Selected[i]].points[j], Text_Set[Controller.Selected[i]].texts[j]);
                }
            }
            temptx.updateMBR();
            DeleteText();
            Text_Set.Add(temptx);
            Controller.Selected = new List<int>();
            Controller.Selected.Add(Text_Set.Count() - 1);
            return true;
        }

        public bool unUnion()
        {
            try
            {
                for (int i = 0; i < Text_Set[Controller.Selected[0]].points.Count(); i++)
                {
                    CText temp = new CText();
                    //完善生成ID的步骤、重新生成外包矩形等操作，调用一些函数。。。
                    temp.newCtext(IDGenerator.TextID(), Text_Set[Controller.Selected[0]].LegendID);
                    temp.addpoint_text(Text_Set[Controller.Selected[0]].points[i], Text_Set[Controller.Selected[0]].texts[i]);
                    temp.updateMBR();
                    Text_Set.Add(temp);
                }
                Text_Set.RemoveAt(Controller.Selected[0]);
                Controller.Selected = new List<int>();
                Controller.Selected.Add(Text_Set.Count() - 1);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public Bitmap drawtext()
        {
            txlayer = new Bitmap(Controller.View_Width, Controller.View_Height);
            Graphics a = Graphics.FromImage(txlayer);
            for (int i = 0; i < Text_Set.Count(); i++)
            {
                CTextLegend ctl = (CTextLegend)Controller.MapLegends[Text_Set[i].LegendID];
                Text_Set[i].textfont = ctl.font;
                Text_Set[i].textbrush = new SolidBrush(ctl.string_Color);
                //Graphics a = Graphics.FromImage(bmp);
                for (int j = 0; j < Text_Set[i].points.Count(); j++)
                {
                    SizeF textsize = a.MeasureString(Text_Set[i].texts[j], Text_Set[i].textfont);
                    Point screen = new Point();
                    screen = Transformer.ToScreen(Text_Set[i].points[j]);
                    Point realtextp = new Point();
                    realtextp.X = screen.X - (int)textsize.Width / 2;
                    realtextp.Y = screen.Y - (int)textsize.Height / 2;
                    a.DrawString(Text_Set[i].texts[j], Text_Set[i].textfont, Text_Set[i].textbrush, realtextp);
                }
            }
            if (Controller.Selected != null)
            {
                if (Controller.Selected.Count() > 0)
                {
                    Point screen1 = new Point();
                    Point screen2 = new Point();
                    Point screen3 = new Point();
                    Point screen4 = new Point();
                    Pen boxpen=new Pen(Color.SkyBlue,2);
                    float[] dashValues = { 3, 3 };
                    boxpen.DashPattern = dashValues;
                    for (int i = 0; i < Controller.Selected.Count(); i++)
                    {
                        for (int j = 0; j < Text_Set[Controller.Selected[i]].texts.Count(); j++)
                        {
                            screen1 = Transformer.ToScreen(Text_Set[Controller.Selected[i]].textboxes[j].lupt);
                            screen2 = Transformer.ToScreen(Text_Set[Controller.Selected[i]].textboxes[j].rupt);
                            screen3 = Transformer.ToScreen(Text_Set[Controller.Selected[i]].textboxes[j].rbpt);
                            screen4 = Transformer.ToScreen(Text_Set[Controller.Selected[i]].textboxes[j].lbpt);
                            a.DrawLine(boxpen, screen1, screen2);
                            a.DrawLine(boxpen, screen2, screen3);
                            a.DrawLine(boxpen, screen3, screen4);
                            a.DrawLine(boxpen, screen4, screen1);
                        }
                    }
                    if (Controller.selectsimple != null)
                    {
                        if (Controller.selectsimple.Count() > 0)
                        {
                            boxpen.Color = Color.Red;
                            for (int i = 0; i < Controller.selectsimple.Count(); i++)
                            {
                                screen1 = Transformer.ToScreen(Text_Set[Controller.Selected[0]].textboxes[Controller.selectsimple[i]].lupt);
                                screen2 = Transformer.ToScreen(Text_Set[Controller.Selected[0]].textboxes[Controller.selectsimple[i]].rupt);
                                screen3 = Transformer.ToScreen(Text_Set[Controller.Selected[0]].textboxes[Controller.selectsimple[i]].rbpt);
                                screen4 = Transformer.ToScreen(Text_Set[Controller.Selected[0]].textboxes[Controller.selectsimple[i]].lbpt);
                                a.DrawLine(boxpen, screen1, screen2);
                                a.DrawLine(boxpen, screen2, screen3);
                                a.DrawLine(boxpen, screen3, screen4);
                                a.DrawLine(boxpen, screen4, screen1);
                            }
                        }
                    }
                }
            }
            return txlayer;
        }
  
        public void SelectCtext(PointF e)
        {
            for (int i = 0; i < Text_Set.Count(); i++)
            {
                float trans;
                if (Controller.MyMap.Unit == "英寸")
                    trans = (float)1 / 96;
                else
                    trans = (float)2.54 / 96;
                float Buffer = 10 * trans;
                for (int j = 0; j < Text_Set[i].texts.Count(); j++)
                {
                    List<PointF> RecBuffer = new List<PointF>();
                    RecBuffer.Add(Text_Set[i].textboxes[j].lbpt);
                    RecBuffer.Add(Text_Set[i].textboxes[j].lupt);
                    RecBuffer.Add(Text_Set[i].textboxes[j].rupt);
                    RecBuffer.Add(Text_Set[i].textboxes[j].rbpt);
                    RecBuffer.Add(Text_Set[i].textboxes[j].lbpt);
                    if (Controller.IfPointInPolygon(RecBuffer, new PointF(e.X, e.Y)))
                    {
                        for (int m = 0; m < Controller.Selected.Count(); m++)
                        {
                            if (i == Controller.Selected[m])
                            {
                                Text_Set[i].isselected = false;
                                Controller.Selected.RemoveAt(m);
                                return;
                            }
                        }
                        Controller.SelectedObjectType = "Text";
                        Controller.Selected.Add(i);
                        Text_Set[i].isselected = true;
                        return;
                    }
                }
            }
        }

    }
}
